package org.lds.scheduled.job.admin.starter.health;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lds.scheduled.job.admin.business.appinstance.model.request.AppInstanceBaseListPageRequest;
import org.lds.scheduled.job.admin.business.appinstance.model.request.AppInstanceBaseUpdateRequest;
import org.lds.scheduled.job.admin.business.appinstance.model.response.AppInstanceBaseResponse;
import org.lds.scheduled.job.admin.business.appinstance.service.AppInstanceBaseService;
import org.lds.scheduled.job.common.model.dto.ResultDTO;
import org.lds.scheduled.job.common.model.db.PageResult;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class AppInstanceHealthCheckStarter extends AbstractInstanceHealthCheckStarter<AppInstanceBaseResponse> {
    private final AppInstanceBaseService appInstanceBaseService;

    @Override
    protected ResultDTO<PageResult<AppInstanceBaseResponse>> pageInstances() {
        AppInstanceBaseListPageRequest request = new AppInstanceBaseListPageRequest();
        request.setGtId(getId());
        request.setPageNum(1);
        request.setPageSize(500);
        request.setOrderByKey("id");
        request.setOrderByDesc(false);
        return appInstanceBaseService.listAppInstancePage(request);
    }

    @Override
    protected Long id(AppInstanceBaseResponse instance) {
        return instance.getId();
    }

    @Override
    protected String address(AppInstanceBaseResponse instance) {
        return instance.buildAddress();
    }

    @Override
    protected boolean enable(AppInstanceBaseResponse instance) {
        return instance.getEnable();
    }

    @Override
    protected void updateInstance(Long id, boolean enable) {
        appInstanceBaseService.updateAppInstance(AppInstanceBaseUpdateRequest.builder().id(id).enable(enable).build());
        log.info("应用实例状态更新 id={} enable={}", id, enable);
    }

}
